package com.scarecrow.blit2d.rasterizer {
	import com.scarecrow.olga.util.Cache;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class RasterFactory {
		
		private static var rasterCache:Cache;
		public static var smooting:Boolean = true;
		
		public function RasterFactory() {
			
		}
		
		public static function buildAnimationRaster(source:MovieClip, key:String):AnimationRaster {
			if (!rasterCache) rasterCache = new Cache();
			var frameData:Array = rasterCache.getObject(key);
			if (!frameData) {
				var frameData:Array = rasterize(source);
				rasterCache.addObject(frameData, key);
			}
			return new AnimationRaster(frameData, source.loaderInfo.frameRate);
		}
		
		private static function rasterize(source:MovieClip):void {
			var frames:Array = [];
			for (var i:int = 1; i <= source.totalFrames; i++) {
				source.gotoAndStop(i);
				var data:BitmapData = new BitmapData(source.width, source.height, true, 0x00000000);
				data.draw(source, null, null, null, null, smoothing);
				frames.push(data);
			}
			return frames;
		}
	}
}